package com.qmx.qmxapp.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.activity.ContactDetailActivity;
import com.qmx.qmxapp.activity.CustomerGroupActivity;
import com.qmx.qmxapp.adapter.ContactMultipleItemAdapter;
import com.qmx.qmxapp.base.BaseFragment;
import com.qmx.qmxapp.bean.ContactModel;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.data.SectionMultipleItem;
import com.qmx.qmxapp.data.Video;
import com.qmx.qmxapp.okhttp.OkHttpHelper;
import com.qmx.qmxapp.token.TokenManager;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContactsFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private View fragment_contacts;
    private TextView textView;
    private OkHttpHelper mHttpHelper;
    private Gson mGson = new Gson();
    private List<SectionMultipleItem> modelList = new ArrayList<>();
    private Intent intent = new Intent();
    private ContactMultipleItemAdapter sectionAdapter;

    public ContactsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        fragment_contacts = inflater.inflate(R.layout.fragment_contacts, container, false);
        mHttpHelper = OkHttpHelper.getInstance();
        textView = fragment_contacts.findViewById(R.id.tv_search);
        recyclerView = (RecyclerView) fragment_contacts.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        getDepList();

        sectionAdapter = new ContactMultipleItemAdapter(R.layout.def_section_head, modelList);
        sectionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                SectionMultipleItem item = (SectionMultipleItem) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.card_view:
                        // 获取主体item相应数据给后期使用
                        if (item.getVideo() != null && hasLower(item.getVideo().getContent().toString())) {
//                            Toast.makeText(getActivity(), item.getVideo().getContent().toString(), Toast.LENGTH_LONG).show();
                            intent.setClass(getActivity(), CustomerGroupActivity.class);
                            intent.putExtra("title", item.getVideo().getName());
                            intent.putExtra("name", item.getVideo().getImg());
                            intent.putExtra("content", item.getVideo().getContent().toString());
                        } else {
                            intent.setClass(getActivity(), ContactDetailActivity.class);
                            intent.putExtra("content", item.getVideo().getContent().toString());
                        }
                        Log.i("ContactsFragment", item.getVideo().getContent().toString());
                        startActivity(intent);
                        break;
                    default:
                        Toast.makeText(getActivity(), "OnItemChildClickListener " + position, Toast.LENGTH_LONG).show();
                        break;
                }
            }
        });
        recyclerView.setAdapter(sectionAdapter);
        return fragment_contacts;

    }

    public boolean hasLower(String json) {
        if (json == null || "".equals(json)) {
            return false;
        }
        if ((json.contains("[") && json.contains("]"))) {
            return true;
        } else {
            return false;
        }
    }

    public void getDepList() {
        OkGo.<LzyResponse<List<ContactModel>>>post(AppConstant.Url + ComParamContact.contactList.PATH)
                .headers(ComParamContact.tokenCheck.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .execute(new JsonCallback<LzyResponse<List<ContactModel>>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<List<ContactModel>>> response) {
                        String json = mGson.toJson(response.body().data.get(0));
                        ContactModel contactModel = mGson.fromJson(json, ContactModel.class);
                        modelList.clear();
                        if (contactModel.getContent() != null) {
                            if (contactModel.getContent().getDepartment() != null) {
                                Log.e("getDepartment", contactModel.toString());
                                for (ContactModel.Content.Department department : contactModel.getContent().getDepartment()) {
                                    Video video = new Video("department", department.getName(), mGson.toJson(department));
                                    modelList.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
                                }
                            }
                            modelList.add(new SectionMultipleItem(true, "组织架构（启明星集团）", false));
                            if (contactModel.getContent().getDiscuss() != null) {
                                for (ContactModel.Content.Discuss discuss : contactModel.getContent().getDiscuss()) {
                                    Video video = new Video("discuss", discuss.getName(), mGson.toJson(discuss));
                                    modelList.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
                                }
                            }
                            if (contactModel.getContent().getUsers() != null) {
                                for (ContactModel.Content.Users users : contactModel.getContent().getUsers()) {
                                    Video video = new Video(users.getAvatar(), users.getName(), mGson.toJson(users));
                                    modelList.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
                                }
                            }
                        }
                        sectionAdapter.setNewData(modelList);
                        sectionAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Response<LzyResponse<List<ContactModel>>> response) {
                        Log.e("onError", response.toString());
                        //                            handleError(response);
                    }
                });
    }
}
